package com.lyy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyy.Vo.MenuNodeVO;
import com.lyy.Vo.UserKeyWord;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.response.Result;
import com.lyy.system.entity.Menu;
import com.lyy.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单权限模快",tags = "菜单权限接口")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/queryMenuList")
    @ApiOperation(value = "菜单列表",notes = "菜单列表")
    public Result queryMenuList(@RequestBody UserKeyWord userKeyWord){
        List<MenuNodeVO> menuList= menuService.queryMenuList(userKeyWord);
        return Result.ok().data("menuList",menuList);
    }

    @PostMapping("/getMenuByRoleId")
    @ApiOperation(value = "菜单列表(根据角色ID)",notes = "根据角色查询菜单列表")
    public Result getMenuByRoleId(Long id){
        QueryWrapper<Menu> wrapper =new QueryWrapper<>();
        wrapper.eq("trm.role_id",id).eq("tm.available","1");
        Set<Long> menuList =menuService.getMenuByRoleId(wrapper);
        return Result.ok().data("menuList",menuList);
    }

    @ApiOperation(value = "查询菜单详情",notes = "菜单详情")
    @PostMapping("/edit")
    public Result edit(Long id){
        Menu menu =menuService.edit(id);
        return Result.ok().data("menuDetail",menu);
    }

    @ControllerEndpoint(exceptionMessage = "新增菜单失败", operation = "新增菜单")
    @ApiOperation(value = "添加菜单",notes = "添加菜单信息")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu){
        menuService.addMenu(menu);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "更新菜单失败", operation = "更新菜单")
    @ApiOperation(value = "更新菜单",notes = "更新菜单信息")
    @PostMapping("/updataMenu")
    public Result updataMenu(@RequestBody Menu menu){
        menuService.updateMenu(menu);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "删除菜单失败", operation = "删除菜单")
    @ApiOperation(value = "删除菜单",notes = "删除菜单信息")
    @DeleteMapping("/deleteMenu")
    public Result deleteMenu(Long id){
        menuService.deleteMenu(id);
        return Result.ok();
    }
}
